package com.sidianzhong.sdz.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
public class WalletProcess {


	private  static String coin_usdt_paths;

	@Value(value = "${constants.coin_usdt_path}")
	public void setCoin_usdt_path(String coin_usdt_path){
		coin_usdt_paths = coin_usdt_path;
	}


	/**
	 * 创建BTC钱包地址
	 * @return
	 */
	public WalletAccount createUSDTAccount() {
		String path = coin_usdt_paths + "createAccount";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		RequestBody body = new FormBody.Builder().build();
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();
		System.out.println("request++++"+request);
		System.out.println(request.header("tag"));//null
		System.out.println(request.method());
		System.out.println(request.url());
		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string+"^^^^^^^^");
				JSONObject tmpobject = JSON.parseObject(string);
				String result = tmpobject.getString("result");
				JSONObject jsonObject = JSON.parseObject(result);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					walletAccount.setWalletEthAddress(jsonObject.getString("ETHaddress"));
					walletAccount.setWalletEthPrivatekey(jsonObject.getString("ETHprivateKey"));
					walletAccount.setResult(true);
					return walletAccount;
				}
				System.out.println("return  createAccount error");
				walletAccount.setResult(false);
				return walletAccount;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}

	/**
	 * 用户ETH-USDT转账
	 * @param toaddress
	 * @param value
	 * @return
	 */
	public WalletAccount withdrawEUSDT(String toaddress, double value) {
		String path = coin_usdt_paths + "withdrawUSDT";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("address", toaddress);
		jsonObject.put("value", value);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					String resultHash = tmpobject.getString("result");
					walletAccount.setResult(true);
					walletAccount.setHash(resultHash);
					return walletAccount;
				}
				//
				System.out.println("return withDrawAKB error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}

	/**
	 * 检查ETH-USDT交易状态
	 */
	//获取hash状态 0成功，1失败，2正在进行
	public WalletAccount DoCheckTxEUSDT(String hash) {
		String path = coin_usdt_paths + "checkTx";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("hash", hash);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					int resultRet = tmpobject.getIntValue("result");
					walletAccount.setHashStatus(resultRet);
					walletAccount.setResult(true);
					return walletAccount;
				}
				//
				System.out.println("return  checkTx error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}
}
